
import { RuleState } from './types';
import type { FilterRuleOptions, TypeConfig, RuleGenerationResult } from './types';
import { globalFilterConfigs } from '../../components/options/filterOptions';

const globalConfig: TypeConfig = {
    class: null,
    icon: 'White',
    iconShape: 'Circle',
    name: '全局设置',
    fontSize: 32
};

export function generateGlobalRule(options: FilterRuleOptions): RuleGenerationResult {
    const { label, value, soundEnabled } = options;
    const config = globalFilterConfigs[value as keyof typeof globalFilterConfigs];

    let result = `Show # ${globalConfig.name}-${label}\n`;

    // 添加基础条件
    result += `  Rarity ${value === 'Normal Items' ? 'Normal' : value === 'Magic Items' ? 'Magic' : 'Rare'}\n`;

    // 添加物品等级范围
    if (config.itemLevelRange.max) {
        result += `  ItemLevel >= ${config.itemLevelRange.min} <= ${config.itemLevelRange.max}\n`;
    } else {
        result += `  ItemLevel >= ${config.itemLevelRange.min}\n`;
    }

    // 添加装备类型过滤
    if (config.allowedTypes && config.allowedTypes.length > 0) {
        result += "  Class " + config.allowedTypes.map(type => `"${type}"`).join(" ") + "\n";
    }

    // 添加品质和大小限制
    result += `  Quality >= ${config.minQuality} # 最低品质要求\n`;
    if (config.maxSize) {
        result += `  Width <= ${config.maxSize.width} # 限制宽度\n`;
        result += `  Height <= ${config.maxSize.height} # 限制高度\n`;
    }

    // 添加连接孔要求
    if (config.minSockets) {
        result += `  LinkedSockets >= ${config.minSockets} # 最少连接数\n`;
        result += "  SocketGroup RGB # 三色链接\n";
    }

    // 添加装备基础属性要求
    if (config.requirements) {
        result += "\n  # 装备基础属性要求\n";
        const reqs = config.requirements;
        if (reqs.minDefense) {
            result += `  BaseDefencePercentile >= ${reqs.minDefense} # 基础防御\n`;
        }
        if (reqs.minArmour) {
            result += `  BaseArmour >= ${reqs.minArmour} # 基础护甲\n`;
            result += `  HasExplicitMod "increased Armour" # 增加护甲词缀\n`;
        }
        if (reqs.minEvasion) {
            result += `  BaseEvasion >= ${reqs.minEvasion} # 基础闪避\n`;
            result += `  HasExplicitMod "increased Evasion Rating" # 增加闪避词缀\n`;
        }
        if (reqs.minEnergyShield) {
            result += `  BaseEnergyShield >= ${reqs.minEnergyShield} # 基础能量护盾\n`;
            result += `  HasExplicitMod "increased Energy Shield" # 增加能量护盾词缀\n`;
        }
    }

    // 添加装备特殊要求
    if (value === 'Rare Items') {
        result += "\n  # 稀有装备特殊要求\n";
        result += "  HasExplicitMod >= 4 # 至少4个词缀\n";
        result += "  HasResistance >= 20 # 至少20%抗性\n";
        result += "  HasInfluence Elder Shaper # 支持特殊影响\n";
    }

    // 添加装备等级和品质要求
    result += "\n  # 装备等级和品质要求\n";
    if (value === 'Magic Items' || value === 'Rare Items') {
        result += "  ItemLevel >= 60 # 物品等级要求\n";
        result += "  GemLevel >= 16 # 技能石等级要求\n";
        result += "  AlternateQuality True # 支持替代品质\n";
    }

    // 添加样式设置
    result += "\n  # 视觉样式设置\n";

    // 根据显示状态设置样式
    if (options.displayEnabled) {
        result += `  SetTextColor ${config.textColor} # 文字颜色\n`;
        result += `  SetBackgroundColor 0 0 0 0 # 透明背景\n`;
        result += `  SetBorderColor ${config.textColor} # 边框颜色\n`;
        result += `  SetFontSize ${config.fontSize} # 字体大小\n`;
    } else {
        // 如果显示开关关闭，使用较小的字体和较暗的颜色
        result += `  SetTextColor 128 128 128 # 暗灰色文字\n`;
        result += `  SetBackgroundColor 0 0 0 0 # 透明背景\n`;
        result += `  SetBorderColor 128 128 128 # 暗灰色边框\n`;
        result += `  SetFontSize ${Math.max(20, config.fontSize - 20)} # 较小字体\n`;
    }

    // 根据过滤状态添加过滤规则
    if (options.filterEnabled) {
        result += "\n  # 过滤条件\n";
        result += "  Continue # 继续处理后续规则\n";
    }

    // 添加图标和特效
    result += "\n  # 小地图图标和特效设置\n";
    result += `  MinimapIcon ${config.iconSize} ${config.icon} ${config.iconShape} # 小地图图标\n`;
    if (config.playEffect) {
        result += `  PlayEffect ${config.icon} # 掉落特效\n`;
    }

    // 添加音效设置
    result += "\n  # 音效设置\n";
    if (soundEnabled && config.alertSound) {
        result += `  PlayAlertSound ${config.alertSound.id} ${config.alertSound.volume} # 物品音效\n`;
        result += `  CustomAlertSound "音效\\${label}.mp3" # 自定义音效\n`;
    } else {
        result += "  DisableDropSound True # 禁用掉落音效\n";
        result += "  DisableAlertSound True # 禁用提示音效\n";
    }

    // 添加结束注释
    result += `\n# ==== 优先级: ${config.priority} ====\n`;

    return {
        content: result,
        state: options.filterEnabled && options.displayEnabled ? RuleState.Enabled
            : options.filterEnabled ? RuleState.FilterOnly
                : options.displayEnabled ? RuleState.DisplayOnly
                    : RuleState.Disabled,
        isValid: true
    };
}